﻿using IRepositoryBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RepositoryEntity;
using RepositoryEntity.Extension;
using RepositorySystem.Models;
using RepositorySystem.Models.ConsumableRecordViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace RepositorySystem.Controllers
{
    public class ConsumableRecordController : Controller
    {
        private IConsumableRecordBll _consumableRecordBll;
        private IConsumableInfoBll _consumableInfoBll;
        public ConsumableRecordController(IConsumableRecordBll consumableRecordBll, IConsumableInfoBll consumableInfoBll)
        {
            _consumableRecordBll = consumableRecordBll;
            _consumableInfoBll = consumableInfoBll;
        }

        /// <summary>
        /// 耗材记录信息表
        /// </summary>
        /// <returns></returns>
        public IActionResult ConsumableRecordListView()
        {
            return View();
        }


        /// <summary>
        /// 添加耗材记录页面
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IActionResult AddConsumableRecordView()
        {
            return View();
        }

        /// <summary>
        /// 添加耗材记录信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddConsumableRecord(string consumbleId, int num)
        {

            CustomActionResult res = new CustomActionResult();

            //string userInfoJson = HttpContext.Session.GetString("UserInfo");
            //UserInfo userInfo = JsonConvert.DeserializeObject<UserInfo>(userInfoJson);
            UserInfo userInfo = this.GetSessionUserInfo();
            if (userInfo == null)
            {
                res.Msg = "未获取到登录用户的信息";
                return Json(res);
            }
            if (string.IsNullOrEmpty(consumbleId))
            {
                res.Msg = "耗材不能为空";
                return Json(res);
            }
            else if (num == 0)
            {
                res.Msg = "数量不能为空";
                return Json(res);
            }
            ConsumableRecord consumableRecord = new ConsumableRecord()
            {
                ID = Guid.NewGuid().ToString(),
                ConsumbleId = consumbleId,
                Num = num,
                Type = 1,
                CreateTime = DateTime.Now,
                Creator = userInfo.ID

            };
            bool isSuccess = _consumableRecordBll.AddConsumableRecord(consumableRecord);
            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Msg = "成功";
                return Json(res);
            }
            else
            {
                return Json(res);
            }



        }

        /// <summary>
        /// 获取耗材记录集合、分页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="ConsumbleId"></param>
        /// <returns></returns>
        public IActionResult GetConsumableRecordList(int page, int limit, string ConsumbleId)
        {
            int count;
            object consumableRecord = _consumableRecordBll.GetConsumableRecordListByPage(page, limit, out count, ConsumbleId);

            return Json(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = consumableRecord
            });
        }

        /// <summary>
        /// 获取耗材管理的耗材名字
        /// </summary>
        /// <returns></returns>
        public IActionResult GetConsumableInfoSelectOption()
        {
            CustomActionResult res = new CustomActionResult();
            object options = _consumableInfoBll.GetConsumableInfoSelectOption();
            res.IsSuccess = true;
            res.Msg = "成功";
            res.Status = 1;
            res.Data = options;
            return Json(res);
        }

        ///// <summary>
        ///// 软删除多个耗材记录
        ///// </summary>
        ///// <param name="consumableRecordIds"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public IActionResult DeleteConsumableRecords(List<string> consumableRecordIds)
        //{
        //    CustomActionResult res = new CustomActionResult();
        //    if (consumableRecordIds == null || consumableRecordIds.Count == 0)
        //    {
        //        res.Msg = "请选择要删除的数据";
        //    }
        //    else
        //    {
        //        _consumableRecordBll.SoftDeleteConsumableRecords(consumableRecordIds);
        //        res.Status = 1;
        //        res.Msg = "成功";
        //        res.IsSuccess = true;
        //    }

        //    return Json(res);
        //}


        public IActionResult DeleteConsumableRecords(string consumbleId)
        {
            CustomActionResult res = new CustomActionResult();
            if (_consumableRecordBll.DeleteConsumableRecords(consumbleId))
            {
                res.Status = 1;
                res.Msg = "成功";
                res.IsSuccess = true;
                return Json(res);
            }
            else
            {
                return Json(res);
            }
        }

        //文件上传
        public IActionResult Upload(List<IFormFile> files)
        {
            CustomActionResult res = new CustomActionResult();
            var file = files.FirstOrDefault();
            if (file != null)
            {
                var userInfoJson = HttpContext.Session.GetString("UserInfo");
                UserInfo userInfo = JsonConvert.DeserializeObject<UserInfo>(userInfoJson);
                if (userInfo == null)
                {

                    return Json(res);
                }
                var stream = file.OpenReadStream();
                //获取上传excel文件，提交至数据库中
                string msg;
                bool isSuccess = _consumableRecordBll.Upload(stream, userInfo.ID, out msg);
                if (isSuccess)
                {
                    res.IsSuccess = true;
                    res.Status = 1;
                    res.Msg = "上传成功";
                    return Json(res);
                }

            }


            return Json(res);
        }
        /// <summary>
        /// 文件下载
        /// </summary>
        /// <returns></returns>
        public IActionResult Download()
        {
            //耗材记录集合
            Stream stream = _consumableRecordBll.GetDownloadDatas();
            return File(stream, "application/octet-stream", "output.xlsx");
        }


    }
}
